﻿namespace Enterprise.Utility
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;

    public static class ModelConvertHelper<T> where T: new()
    {
        public static T ToModel(DataRow dr)
        {
            string name = "";
            T local = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
            PropertyInfo[] properties = local.GetType().GetProperties();
            DataTable table = dr.Table;
            foreach (PropertyInfo info in properties)
            {
                name = info.Name;
                if (table.Columns.Contains(name) && info.CanWrite)
                {
                    object obj2 = dr[name];
                    if ((obj2 != null) && (obj2 != DBNull.Value))
                    {
                        if (info.PropertyType.IsEnum)
                        {
                            info.SetValue(local, Enum.Parse(info.PropertyType, obj2.ToString().Trim(), true), null);
                        }
                        else
                        {
                            info.SetValue(local, obj2, null);
                        }
                    }
                }
            }
            return local;
        }

        public static T ToModel(IDataRecord dr)
        {
            string key = "";
            T local = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
            PropertyInfo[] properties = local.GetType().GetProperties();
            int fieldCount = dr.FieldCount;
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            for (int i = 0; i < fieldCount; i++)
            {
                string str2 = dr.GetName(i).ToLower();
                dictionary[str2] = dr[i];
            }
            foreach (PropertyInfo info in properties)
            {
                key = info.Name.ToLower();
                if (dictionary.ContainsKey(key) && info.CanWrite)
                {
                    object obj2 = dictionary[key];
                    if (obj2 != DBNull.Value)
                    {
                        if (info.PropertyType.IsEnum)
                        {
                            info.SetValue(local, Enum.Parse(info.PropertyType, obj2.ToString().Trim(), true), null);
                        }
                        else
                        {
                            info.SetValue(local, obj2, null);
                        }
                    }
                }
            }
            return local;
        }

        public static IList<T> ToModels(DataTable dt)
        {
            if ((dt == null) || (dt.Rows.Count == 0))
            {
                return null;
            }
            IList<T> list = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T local2 = default(T);
                T local = (local2 == null) ? Activator.CreateInstance<T>() : default(T);
                foreach (PropertyInfo info in local.GetType().GetProperties())
                {
                    string name = info.Name;
                    if (dt.Columns.Contains(name) && info.CanWrite)
                    {
                        object obj2 = row[name];
                        if ((obj2 != null) && (obj2 != DBNull.Value))
                        {
                            if (info.PropertyType.IsEnum)
                            {
                                info.SetValue(local, Enum.Parse(info.PropertyType, obj2.ToString().Trim(), true), null);
                            }
                            else
                            {
                                info.SetValue(local, obj2, null);
                            }
                        }
                    }
                }
                list.Add(local);
            }
            return list;
        }

        public static IList<T> ToModels(IDataReader dr)
        {
            IList<T> list = new List<T>();
            while (dr.Read())
            {
                list.Add(ModelConvertHelper<T>.ToModel(dr));
            }
            return list;
        }
    }
}

